/**
 * 
 */
package com.alstudio.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import com.alstudio.app.LocalEnv;
import com.alstudio.net.EventHandler.XmppEventHandler;
import com.alstudio.net.protocol.xmpp.XmppAdapter;
import com.alstudio.net.protocol.xmpp.data.XmppData;
import com.alstudio.ui.BaseActivity;
import com.alstudio.utils.Mylog;
import com.alstudio.view.NoticeItem;
import com.alstudio.view.listener.NoticeItemManagerListener;

/**
 * @author Alonso Lee
 *
 */
public class BackgroundService extends Service implements NoticeItemManagerListener{
	
	private XmppData data;
	private XmppEventHandler mXmppEventHandler;
	private final String TAG = "BackgroundService";
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		initService();

	}
	
	private void initService()
	{
		mXmppEventHandler = new XmppEventHandler();
		LocalEnv.adapter = new XmppAdapter(mXmppEventHandler);

		data = new XmppData("117.135.132.61", "5555","mk",50);
		data.setUsername("1333706399");
		data.setPassword("000000");
		LocalEnv.adapter.xmppLogin(data);

//		mHandler.sendEmptyMessageDelayed(1, 4*1000);
		LocalEnv.mNoticeItemManager.setNoticeItemManager(this);
		
		
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	private Handler mHandler = new Handler(){/*
		public void handleMessage(android.os.Message msg) {
			switch(msg.what)
			{
			case 0:
				ArrayList<BaseActivity> mListeners = LocalEnv.mXmppEventNotifyManager.getXmppEventListeners();
				if(mListeners != null)
				{
					for(BaseActivity b:mListeners)
					{
						if(b.isCallbackEnable())
						{
							b.handleLoginedEvent();
						}
						else
						{
							Mylog.i("BackgroundService","ignore notify " + b.activityName);
						}
					}
				}
				break;
			case 1:
				adapter.xmppLogin(data);
				break;
			}

		};
	*/};

	@Override
	public void onNewNoticeItemAdded(NoticeItem item) {
		// TODO Auto-generated method stub
		if(BaseActivity.instance != null)
		{
			Mylog.d(TAG,"calling "+BaseActivity.instance.activityName);
			BaseActivity.instance.onNewNoticeItemAdded(item);
		}
	}

	@Override
	public void onNoticeItemRemoved(NoticeItem item) {
		// TODO Auto-generated method stub
		if(BaseActivity.instance != null)
		{
			Mylog.d(TAG,"calling "+BaseActivity.instance.activityName);
			BaseActivity.instance.onNoticeItemRemoved(item);
			
			NoticeItem nextItem = LocalEnv.mNoticeItemManager.getNextNoticeItem();
			if(nextItem != null)
			{
				onNewNoticeItemAdded(nextItem);
			}
		}
	}

	@Override
	public void onNoticeItemListEmpty() {
		// TODO Auto-generated method stub
		if(BaseActivity.instance != null)
		{
			Mylog.d(TAG,"calling "+BaseActivity.instance.activityName);
			BaseActivity.instance.onNoticeItemListEmpty();
		}
	}

}
